using System.Collections.Generic;
using Gameplay.PVE.Entity;
using Gameplay.PVE.Utils;
using Unity.Mathematics;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.PlayerLoop;
using Yoozoo.Libs;
using Yoozoo.Managers;
using Random = UnityEngine.Random;

namespace Gameplay.PVE
{
    public class TowerMoveComponent:IComponent,ITransmit
    {
        //位置相关控件
        private UnitBase unit;
        
        private ControlBehaviour controlBehaviour;
        private float startSpawnTime;

        void IClass.OnReset()
        {
            if (controlBehaviour != null)
            {
                ClassManager.Free(controlBehaviour);
                controlBehaviour = null;
            }

            startSpawnTime = 0;
        }

        void IComponent.OnInitialize(UnitBase unit)
        {
            this.unit = unit;
        }

        void IComponent.OnUpdate()
        {
            if (unit.Data.isDead)
            {
                return;
            }
            if (startSpawnTime != 0)
            {
                float spawnDelay = 2f;
                if (TimeManager.logicTime - startSpawnTime > spawnDelay)
                {
                    startSpawnTime = 0;
                    unit.Data.isShowingSpawn = false;
                    unit.Transmit(ETransmitType.ShowHud);
                }
            }

            UpdateForward();
            unit.gameObject.transform.position = unit.Data.position;
            unit.gameObject.transform.forward = unit.Data.forward;
        }

        void IComponent.OnBirth()
        {
            
            unit.Data.forward = Vector3.forward;
            unit.Data.position = unit.Data.birthPosition;
            unit.Data.SetTargetPosition(unit.Data.birthPosition);
            unit.gameObject.transform.position = unit.Data.position;
            if (unit.Data.unitType == 0)
            {
                unit.Data.position = unit.Data.birthPosition;
                if (PveManager.Instance.isInRpg)
                {
                    unit.Data.SetTargetForward(new Vector3(1 - unit.Data.team, 0, 0).normalized);
                    unit.Data.forward = unit.Data.targetForward;
                }
                else
                {
                    if  (unit.Data.team == 0)
                    {
                        unit.Data.SetTargetForward(Vector3.right);
                        unit.Data.forward = Vector3.right;
                    }
                    else
                    {
                        unit.Data.SetTargetForward(new Vector3(Random.Range(-1f, 1f), 0, Random.Range(-1f, 1f)).normalized);
                        unit.Data.forward = unit.Data.targetForward;
                    }
                    
                }
               
            }
           
            controlBehaviour = ClassManager.Get<ControlBehaviour>();
            controlBehaviour.Init(unit);
            unit.gameObject.transform.position = unit.Data.position;
            unit.gameObject.transform.forward = unit.Data.forward;
        }

        void IComponent.OnDead()
        {
            
        }

        private void UpdateForward()
        {
            if (unit.Data.targetForward != unit.Data.forward)
            {
                float diffAngle = PveUtils.GetAngle(unit.Data.forward,unit.Data.targetForward);
                if (diffAngle != 0)
                {
                    float angleSpeed = unit.Data.angleSpeed;
                    if (PveManager.Instance.isInRpg && unit.Data.isManualMoving)
                    {
                        angleSpeed = 1000;
                    }
                    float angle = angleSpeed * TimeManager.GameDeltaTime * diffAngle / Mathf.Abs(diffAngle);
                    if (Mathf.Abs(diffAngle) <= Mathf.Abs(angle))
                    {
                        unit.Data.forward = unit.Data.targetForward;
                    }
                    else
                    {
                        unit.Data.forward = Quaternion.AngleAxis(angle, Vector3.up) * unit.Data.forward;
                    }
                }
                else
                {
                    unit.Data.forward = unit.Data.targetForward;
                }
            }
        }


        void ITransmit.OnTransmit(ETransmitType type, BaseTransmitArg arg)
        {
            if (type == ETransmitType.AddControl)
            {
                controlBehaviour.AddControl(arg as AddControlArg);
            }
            else if (type == ETransmitType.RemoveControl)
            {
                controlBehaviour.RemoveControl(arg as RemoveControllArg);
            }
            else if (type == ETransmitType.SpawnInStage)
            {
                unit.Data.isShowingSpawn = true;
                startSpawnTime = TimeManager.logicTime;
                unit.Play("Cele");
            }
        }
        
    }
}